package o;

import android.app.Activity;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class djk {
    private static djk ˊ;
    private static final Object ˋ = new Object();
    private List<WeakReference<Activity>> ˏ = new CopyOnWriteArrayList();
    private List<Class> ॱ = new ArrayList();

    private djk() {
    }

    public static djk ˋ() {
        djk o_djk;
        synchronized (ˋ) {
            if (ˊ == null) {
                ˊ = new djk();
            }
            o_djk = ˊ;
        }
        return o_djk;
    }

    public void ˎ(Class cls) {
        if (!this.ॱ.contains(cls)) {
            this.ॱ.add(cls);
        }
    }

    private boolean ˏ(Activity activity) {
        for (Class isInstance : this.ॱ) {
            if (isInstance.isInstance(activity)) {
                return true;
            }
        }
        return false;
    }

    public synchronized void ˎ(Activity activity) {
        if (activity != null) {
            if (ˏ(activity)) {
                Logger.ˏ("ActivitySizeController", "addInstance activity is instanceof MainActivityBase");
            }
        }
        if (this.ˏ.size() >= 5) {
            WeakReference ˊ = ˊ(this.ˏ, 0);
            ˏ(this.ˏ, 0);
            if (ˊ != null) {
                Activity activity2 = (Activity) ˊ.get();
                if (activity2 != null) {
                    activity2.finish();
                    Logger.ˎ("ActivitySizeController", "list remove i=" + 0);
                }
            }
        }
        this.ˏ.add(new WeakReference(activity));
        Logger.ˎ("ActivitySizeController", "addInstance size=" + this.ˏ.size());
    }

    private void ˏ(List list, int i) {
        if (list.size() > i) {
            list.remove(i);
        }
    }

    private WeakReference<Activity> ˊ(List<WeakReference<Activity>> list, int i) {
        if (list.size() > i) {
            return (WeakReference) list.get(i);
        }
        return null;
    }

    public synchronized void ˊ(Activity activity) {
        if (!CollectCheck.isEmpty(this.ˏ)) {
            for (WeakReference weakReference : this.ˏ) {
                Activity activity2 = (Activity) weakReference.get();
                if (activity2 != null && activity2 == activity) {
                    this.ˏ.remove(weakReference);
                    Logger.ˎ("ActivitySizeController", "removeInstance");
                    break;
                }
            }
        }
    }
}
